package cn.rocky.prepare;

import java.util.Arrays;
import java.util.Comparator;

/**
 * 27. 移除元素
 */
public class removeElement {

    public int removeElement(int[] nums, int val) {
        int len = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == val) {
                nums[i] = 0;
            } else {
                len++;
            }
        }
        return len;
    }

    public int removeElement1(int[] nums, int val) {
        int left = 0;
        int right = nums.length;
        while (left < right) {
            if (nums[left] == val) {
                nums[left] = nums[right - 1];
                right--;
            } else {
                left++;
            }
        }
        return left;

    }

    public int removeElement2(int[] nums, int val) {
        int left = 0;
        int n = nums.length;
        for (int right = 0; right < n;right++) {
            if (nums[right]!=val) {
                nums[left] = nums[right];
                left++;
            }
        }
        return left;
    }
}
